﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ykglxt
{
    public partial class Form_RYZD : Form
    {
        public Form_RYZD()
        {
            InitializeComponent();
        }

        private void Form_RYZD_Load(object sender, EventArgs e)
        {
            
            try
            {
                this.dic_KSTableAdapter.Fill(this.yKGLXTDataSet.Dic_KS);
                this.dic_RYTableAdapter.Fill(this.yKGLXTDataSet.Dic_RY);
            }
            catch (SqlException)
            {
                MessageBox.Show("网络故障，请稍后再试！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            int rowindx = (dgvRY.SelectedRows[0].Index - 1 + dgvRY.RowCount) % dgvRY.RowCount;
            dgvRY.ClearSelection();
            dgvRY.Rows[rowindx].Selected = true;
            dgvRY.FirstDisplayedScrollingRowIndex = dgvRY.SelectedRows[0].Index;
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            int rowIndex = (dgvRY.SelectedRows[0].Index + 1) % dgvRY.RowCount;
            dgvRY.ClearSelection();
            dgvRY.Rows[rowIndex].Selected = true;
            dgvRY.FirstDisplayedScrollingRowIndex = rowIndex;
            if (rowIndex < dgvRY.FirstDisplayedScrollingRowIndex)
            {
                dgvRY.FirstDisplayedScrollingRowIndex = rowIndex;
            }
            else if (!dgvRY.Rows[rowIndex].Displayed)
            {
                dgvRY.FirstDisplayedScrollingRowIndex++;
            }

        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            dgvRY.ClearSelection();
            dgvRY.Rows[0].Selected = true;
            dgvRY.FirstDisplayedScrollingRowIndex = 0;
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            dgvRY.ClearSelection();
            dgvRY.Rows[dgvRY.RowCount - 2].Selected = true;
            dgvRY.FirstDisplayedScrollingRowIndex = dgvRY.RowCount - 1;

        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            dgvRY.ClearSelection();
            dgvRY.Rows[dgvRY.NewRowIndex].Selected = true;
            dgvRY.FirstDisplayedScrollingRowIndex = dgvRY.NewRowIndex;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该记录？", "请选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                dgvRY.Rows.RemoveAt(this.dgvRY.CurrentRow.Index);
            }

        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            dgvRY.CurrentCell = null;
            try
            {
                MessageBox.Show("保存成功，已保存" + dic_RYTableAdapter.Update(yKGLXTDataSet.Dic_RY).ToString() + "条修改记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
            catch (SqlException ex)
            {
                if (ex.Number == 2627)
                    MessageBox.Show("保存失败，编码必须是唯一的，请查看是否存在重复的编码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else if (ex.Number == 515)
                    MessageBox.Show("保存失败，编码不能为空，请查看是否存在记录缺填编码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                    MessageBox.Show("保存失败，网络故障，请稍后再试！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
